#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"

trigger-scheduler-docker-local-pre-deploy() {
  declare desc="scheduler-docker-local pre-deploy plugin trigger"
  declare trigger="pre-deploy"
  declare APP="$1" IMAGE_TAG="$2"

  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
    return
  fi

  scheduler-docker-local-pre-deploy-chown-app "$APP" "$IMAGE_TAG"
  scheduler-docker-local-pre-deploy-precheck "$APP" "$IMAGE_TAG"
}

scheduler-docker-local-pre-deploy-chown-app() {
  declare desc="Runs chown against the /app directory for herokuish images"
  declare APP="$1" IMAGE_TAG="$2"
  local DOCKER_RUN_LABEL_ARGS="--label=com.dokku.app-name=$APP"
  local IMAGE DISABLE_CHOWN DOCKER_ARGS DOKKU_APP_TYPE DOKKU_APP_USER APP_PATHS CONTAINER_PATHS
  declare -a ARG_ARRAY

  IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")

  DOKKU_APP_TYPE=$(config_get "$APP" DOKKU_APP_TYPE || true)
  DOKKU_APP_USER=$(config_get "$APP" DOKKU_APP_USER || true)
  DOKKU_APP_USER=${DOKKU_APP_USER:="herokuishuser"}
  APP_PATHS=$(dokku --quiet storage:list "$APP" || true)

  if [[ -n "$APP_PATHS" ]]; then
    CONTAINER_PATHS=$(echo "$APP_PATHS" | awk -F ':' '{ print $2 }' | xargs)
    DOCKER_ARGS=$(: | plugn trigger docker-args-deploy "$APP" "$IMAGE_TAG")
    # strip --restart args from DOCKER_ARGS
    DOCKER_ARGS=$(sed -e "s/--restart=[[:graph:]]\+[[:blank:]]\?//g" <<<"$DOCKER_ARGS")
    eval "ARG_ARRAY=($DOCKER_ARGS)"
  fi

  if [[ "$DOKKU_APP_TYPE" != "herokuish" ]] || [[ -z "$CONTAINER_PATHS" ]]; then
    return
  fi

  DISABLE_CHOWN="$(fn-plugin-property-get "scheduler-docker-local" "$APP" "disable-chown" "")"
  if [[ "$DISABLE_CHOWN" == "true" ]]; then
    return
  fi

  # shellcheck disable=SC2086
  "$DOCKER_BIN" container run "${DOCKER_RUN_LABEL_ARGS[@]}" $DOKKU_GLOBAL_RUN_ARGS "${ARG_ARRAY[@]}" $IMAGE /bin/bash -c "find $CONTAINER_PATHS -not -user $DOKKU_APP_USER -print0 | xargs -0 -r chown -R $DOKKU_APP_USER" || true
}

scheduler-docker-local-pre-deploy-precheck() {
  declare desc="Outputs the checks messages if necessary"
  declare APP="$1" IMAGE_TAG="$2"
  local IMAGE=$(get_deploying_app_image_name "$APP" "$IMAGE_TAG")
  local CHECKS_FILE=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
  trap "rm -rf '$CHECKS_FILE' >/dev/null" RETURN INT TERM
  copy_from_image "$IMAGE" "CHECKS" "$CHECKS_FILE" 2>/dev/null || true

  dokku_log_info2 "Processing deployment checks"
  if [[ ! -s "${CHECKS_FILE}" ]]; then
    local CHECKS_URL="${DOKKU_CHECKS_URL:-http://dokku.viewdocs.io/dokku/deployment/zero-downtime-deploys/}"
    dokku_log_verbose "No CHECKS file found. Simple container checks will be performed."
    dokku_log_verbose "For more efficient zero downtime deployments, create a CHECKS file. See ${CHECKS_URL} for examples"
  fi
}

trigger-scheduler-docker-local-pre-deploy "$@"
